package cn.tedu.bonfire.admin.content.controller;


import cn.tedu.bonfire.admin.content.pojo.dto.AddNewTagDTO;
import cn.tedu.bonfire.admin.content.pojo.dto.TagUpdateNameByIdParam;
import cn.tedu.bonfire.admin.content.pojo.vo.TagListStartVO;

import cn.tedu.bonfire.admin.content.service.ITagService;
import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("admin/tag")
@Api(tags = "2.标签管理")
@Validated
@Slf4j
public class TagController {

    @Autowired
    private ITagService tagService;

    @PostMapping("add-new")
    @ApiOperation("新增标签")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AddNewTagDTO addNewTagDTO){
        tagService.annNew(addNewTagDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除标签")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签id",
                    paramType = "query", dataType = "Long")
    })
    public JsonResult deleteTagById(Long id){
        tagService.deleteTagById(id);
        return JsonResult.ok();
    }

    /**
     * 修改标签名
     * @param tagUpdateNameByIdParam
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改标签")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签id",
                    paramType = "query", dataType = "Long")
    })
    public JsonResult updateTagById(TagUpdateNameByIdParam tagUpdateNameByIdParam){
        tagService.updateTagNameById(tagUpdateNameByIdParam.getId(),tagUpdateNameByIdParam.getTagName());
        return JsonResult.ok();
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @GetMapping("")
    @ApiOperation("标签列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1",
                    paramType = "query", dataType = "int")
    })
    public JsonResult list(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal, Integer page){
        Integer pageNum = page == null ? 1 : page;
        PageData<TagListStartVO> pageData = tagService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    /**
     * ES查询订单列表

     * @return
     */
    @GetMapping("/rebuild-search")
    @ApiOperation("重建标签的搜索数据")
    @ApiOperationSupport(order = 500)
    public JsonResult listSearch(){
        tagService.rebuildSearch();
        return JsonResult.ok();
    }

    @GetMapping("/search")
    @ApiOperation("搜索标签")
    @ApiOperationSupport(order = 510)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码",  paramType = "query", dataType = "int")
    })
    public JsonResult search(String keyword,Integer page){

        PageData<TagListStartVO> searchHits = tagService.search(keyword,page);
        return JsonResult.ok(searchHits);
    }
}
